
/**   
*    
* ��Ŀ��ƣ�a_yongan   
* ����ƣ�UserServiceImpl   
* ��������   
* �����ˣ�liubo   
* ����ʱ�䣺2018��5��30�� ����6:08:19    
* �޸�ʱ�䣺2018��5��30�� ����6:08:19   
* �޸ı�ע��   
* @version    
*    
*/
package com.yongan.user.service.impl;

import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.yongan.user.dao.UserDao;
import com.yongan.user.dto.UserExecution;
import com.yongan.user.dto.UserExposer;
import com.yongan.user.entity.User;
import com.yongan.user.enums.UserStatEnum;
import com.yongan.user.exception.UserException;
import com.yongan.user.service.UserService;

/**
 * @author liubo
 *
 */
@Service
public class UserServiceImpl implements UserService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private UserDao userDao;
	private final String slat = "dsjdajsdkHJJHAJAKdsd921e0p12*DAe#@$@!qew";
	/* ���� Javadoc��
	 * @see com.yongan.user.service.UserService#getUserList(int, int)
	 */
	@Override
	public List<User> getUserList(int offset, int limit) {
		// TODO �Զ���ɵķ������
		List<User> userList = userDao.queryAll(offset, limit);
		return userList;
	}

	/* ���� Javadoc��
	 * @see com.yongan.user.service.UserService#getUserById(java.lang.String)
	 */
	@Override
	public User getUserById(String userId) {
		// TODO �Զ���ɵķ������
		User user = userDao.queryById(userId);
		return user;
	}

	/* ���� Javadoc��
	 * @see com.yongan.user.service.UserService#getUserListByDeptId(java.lang.String)
	 */
	@Override
	public List<User> getUserListByDeptId(String deptId,int offset,int limit) {
		// TODO �Զ���ɵķ������
		List<User> userListById = userDao.queryAllByDeptId(deptId,offset,limit);
		return userListById;
	}

	/* ���� Javadoc��
	 * @see com.yongan.user.service.UserService#exportUserUrl(java.lang.String)
	 */
	@Override
	public UserExposer exportUserUrl(String userId) {
		// TODO �Զ���ɵķ������
		User user = userDao.queryById(userId);
		if (user == null){
			return new UserExposer(false, userId);
		}
		String md5 = getMD5(userId);
		return new UserExposer(true, md5, userId);
	}

	/* ���� Javadoc��
	 * @see com.yongan.user.service.UserService#executeUser(com.yongan.user.entity.User, java.lang.String, java.lang.String)
	 */
	@Override
	public UserExecution executeUser(User user, String userId, String md5) {
		if(md5 == null || !md5.equals(getMD5(userId))){
			//throw new UserException("user data rewrite");
		}
		try{
			if(user == null){
				//������û�����Ϊ�գ�����Ĭ��Ϊɾ�����
				int deleteCount = userDao.deleteUser(userId);
				if(deleteCount<=0){
					throw new UserException("delete failed");
				}else{
					return new UserExecution(userId, UserStatEnum.SUCCESS);
				}
			}else if((userId.equals(""))){
				//������û�����IdΪ�գ�����Ĭ��Ϊ��������
				user.setUserId(UUID.randomUUID().toString());
				int addCount = userDao.insertUser(user);
				if(addCount<=0){
					throw new UserException("add failed");
				}else{
					return new UserExecution(userId, UserStatEnum.SUCCESS);
				}
			}else if(!(user.getUserId()+"").equals("")){
				//������û�����Id��Ϊ�գ�����Ĭ��Ϊ�޸Ĳ���
				int updateCount = userDao.updateUser(user);
				if(updateCount<=0){
					throw new UserException("update failed");
				}else{
					return new UserExecution(user.getUserId(), UserStatEnum.SUCCESS);
				}
			}else{
				throw new UserException("parameter error");
			}
			
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw new UserException("inner error:"+e.getMessage());
		}
	}

	/* ���� Javadoc��
	 * @see com.yongan.user.service.UserService#checkUser(java.lang.String, java.lang.String)
	 */
	@Override
	public User checkUser(String userName, String userPassword) {
		User user = userDao.checkUser(userName, userPassword);
		return user;
	}
	
	@Override
	public User checkUserById(String userId, String userPassword) {
		User user = userDao.checkUserById(userId, userPassword);
		return user;
	}

	private String getMD5(String userId){
		String base = userId + "/" + slat;
		String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
		return md5;
	}
	@Override
	public int getUserCount(String title, String value) {
		return userDao.queryCount(title, value);
	}

	/* （非 Javadoc）
	 * @see com.yongan.user.service.UserService#getCountByDeptId(java.lang.String)
	 */
	@Override
	public int getCountByDeptId(String deptId) {
		return userDao.queryCountByDeptId(deptId);
	}

	/* （非 Javadoc）
	 * @see com.yongan.user.service.UserService#getUserListTree()
	 */
	@Override
	public List<User> getUserListTree() {
		return userDao.queryAllTree();
	}

}
